package com.aaa.product.controller;


import com.aaa.product.dao.TbActInfMapper;
import com.aaa.product.dao.TbActProductMapper;
import com.aaa.product.entity.TbActInf;
import com.aaa.product.entity.TbActProduct;
import com.aaa.product.entity.TbPrdInfo;
import com.aaa.product.service.TbActProductService;
import com.aaa.product.service.TbPrdInfoService;
import com.aaa.product.service.impl.TbActInfServiceImpl;
import com.aaa.product.service.impl.TbPrdInfoServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("activity")
public class TbActInfController {

    @Resource
    TbActInfServiceImpl tbActInfServiceImpl;
    @Resource
    TbActInfMapper tbActInfMapper;
    @Resource
    TbActProductService tbActProductService;
    @Resource
    TbPrdInfoServiceImpl tbPrdInfoServiceImpl;


    @RequestMapping("selectAll")
    public List<TbActInf> selectAll(){
        return tbActInfServiceImpl.listDesc();
    }

    @RequestMapping("saveOrUpdate")
    public boolean saveOrUpdate(TbActInf tbActInf){

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        tbActInf.setUpdateDate(dateString);

        return tbActInfServiceImpl.saveOrUpdate(tbActInf);
    }

    @RequestMapping("delete")
    public boolean delete(Integer tid){
        return tbActInfServiceImpl.removeById(tid);
    }

    @RequestMapping("findById/{tid}")
    public TbActInf findById(@PathVariable String tid){
        return tbActInfServiceImpl.findById(tid);
    }

    //模糊查询
    @RequestMapping("listByLname")
    public List<TbActInf> listByLname(String labelName){
        return tbActInfServiceImpl.listByLname(labelName);
    }


    // 获取图片
    @RequestMapping({"/findProductPicByPath"})
    public void getFeedBackPicture(HttpServletResponse response, @RequestParam("name") String name) throws IOException {
        response.setContentType("image/jpeg;charset=utf-8");
        response.setHeader("Content-Disposition", "inline; filename=girls.png");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\service_product\\src\\main\\resources\\img\\").resolve(name)));
        outputStream.flush();
        outputStream.close();
    }
    // 上传图片
    @RequestMapping({"/insertProductPic"})
    public Object insertProductPic(@RequestParam("file") MultipartFile avatorFile) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            return jsonObject;
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir")+"\\service_product\\src\\main\\resources\\img";
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdir();
            }

            File dest = new File(filePath + System.getProperty("file.separator") + fileName);
            String storePath = fileName;

            try {
                avatorFile.transferTo(dest);
                jsonObject.put("code", 1);
                jsonObject.put("msg", "上传成功");
                jsonObject.put("pic", storePath);
                return jsonObject;
            } catch (IOException var15) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败" + var15.getMessage());
                return jsonObject;
            }
        }
    }
    // 修改图片
    @RequestMapping({"/updateProductPic"})
    public Object updateProductPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("tid") String tid) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            return jsonObject;
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir")+"\\service_product\\src\\main\\resources\\img";
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdir();
            }

            File dest = new File(filePath + System.getProperty("file.separator") + fileName);
            String storePath = fileName;

            try {
                avatorFile.transferTo(dest);
                TbActInf tbActInf = new TbActInf();
                tbActInf.setTid(tid);
                tbActInf.setPicPath(storePath);
                boolean flag = tbActInfServiceImpl.updateById(tbActInf);
                if (flag) {
                    jsonObject.put("code", 1);
                    jsonObject.put("msg", "上传成功");
                    jsonObject.put("pic", storePath);
                    return jsonObject;
                } else {
                    jsonObject.put("code", 0);
                    jsonObject.put("msg", "上传失败");
                    return jsonObject;
                }
            } catch (IOException var15) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败" + var15.getMessage());
                return jsonObject;
            }
        }
    }

    /**
     * 根据活动的编号查询信息
     * @param tid
     * @return
     */
    @RequestMapping("/findTbActProductByTid")
    public TbActInf findTbActProductByTid( String tid){
        TbActInf tbActProducts = tbActInfMapper.getTbActProducts(tid);
        return tbActProducts;
    }

    /**
     * 修改活动里面的单个商品价格
     * @return
     */
    @RequestMapping("/updateActivityPriceById")
    public Object updateActivityPrice(TbActProduct tbActProduct){
        JSONObject jsonObject = new JSONObject();
        boolean result = tbActProductService.updateTbActProductPrice(tbActProduct);
        if (result){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "价格修改成功");
            return jsonObject;
        }
        jsonObject.put("code", 0);
        jsonObject.put("msg", "价格修改失败");
        return jsonObject;
    }
    /**
     * 查询活动里面未添加的商品
     * @return
     */
    @RequestMapping("/findTbActProductByArr")
    public List<TbPrdInfo> findTbActProductByArr(@RequestBody String [] arr){
        for (String s : arr) {
            System.out.println(s);
        }
        List<TbPrdInfo> tbPrdInfos = tbPrdInfoServiceImpl.queryTbPrdInfoByArr(arr);
        return  tbPrdInfos;
    }
    /**
     * 添加活动里面的商品
     * @return
     */
    @RequestMapping("/saveActivityProduct")
    public Object saveActivityProduct(@RequestBody Object [] arr){
        JSONObject jsonObject = new JSONObject();

        boolean result = tbActProductService.saveActivityProduct(arr);
        if (result){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "添加商品成功");
            return jsonObject;
        }
        jsonObject.put("code", 0);
        jsonObject.put("msg", "添加商品失败");
        return jsonObject;
    }

    /**
     * 减商品的库存
     * @return
     */
    @RequestMapping("/subtractStore")
    public boolean subtractStore (@RequestBody Object [] arr2){
        return tbActProductService.subtractStore(arr2);
    }


}
